const express = require('express')
const router = express.Router()


//导入数据库 sqlFn('sql',[],res=>{})
const sqlFn = require('../mysql')

router.get('/getCategory', (req, res) => {
    const sqlLen = "select * from category";
    sqlFn(sqlLen, null, data => {
        let str=''
        data.forEach(el=>{
            console.log()
            str+='{"id":"'+el.id+"\",\"label\":\""+el.name+"\",\"children\":"+JSON.parse(JSON.stringify(el.category_params))+"},"
        })
        str='['+str.slice(0,-1)+']'
        if (str) {
            res.send({
                status: 200,
                data: str
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

router.post('/removeCategory', (req, res) => {
    console.log('req:', req.query);
    const sql = "update category set category_params=? where id=?";
    let arr = [req.query.detail,req.query.id]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: 1,
        });
    })
});

router.post('/editorCategory', (req, res) => {
    console.log('req:', req.query);
    const sql = "update category set category_params=? where id=?";
    let arr = [req.query.detail,req.query.id]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: 1,
        });
    })
});

router.post('/addCategory', (req, res) => {
    console.log('req:', req.query);
    const sql = "update category set category_params=? where id=?";
    let arr = [req.query.detail,req.query.id]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: 1,
        });
    })
});

router.get('/getOrder', (req, res) => {
    const sqlLen = "select * from orders where order_status!=5";
    sqlFn(sqlLen, null, data => {
        // data = data.filter((item,index) => { return item.order_status != 5 })
        // console.log('data:',data)
        data.forEach(el => {
            if(el.order_status==1){
                el.order_status='待发货'
            }else if(el.order_status==2){
                el.order_status='待收货'
            }else if(el.order_status==3){
                el.order_status='已完成'
            }else{
                el.order_status='已取消'
            }
        });
        let len = data.length;
        if (len > 0) {
            res.send({
                status: 200,
                data: data
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


router.get('/getReturned', (req, res) => {
    const sqlLen = "select * from orders where order_status=5";
    sqlFn(sqlLen, null, data => {
        console.log('data:',data)
        // data.forEach(el => {
        //     if(el.returned_status==1){
        //         el.returned_status='待审核'
        //     }else{
        //         el.returned_status='已退款'
        //     }
        // });
        let len = data.length;
        if (len > 0) {
            res.send({
                status: 200,
                data: data
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})
/**
 * 商品查询接口 search
 * 参数：search  {search:xx}
 */
router.get("/order_search", (req, res) => {
    console.log('query:', req.query)
    console.log('传过来的参数:', req.query.search_word)
    var search = req.query.search_word;
    const sql = "select * from orders where concat(`order_number`,`user_name`,`goods_name`) like '%" + search + "%' and order_status!=5";
    sqlFn(sql, null, (result) => {
        console.log('result:', result)
        console.log('result长度:', result.length)
        // result= result.filter((item,index) => { return item.order_status != 5 })
        result.forEach(el => {
            if(el.order_status==1){
                el.order_status='待发货'
            }else if(el.order_status==2){
                el.order_status='待收货'
            }else if(el.order_status==3){
                el.order_status='已完成'
            }else{
                el.order_status='已取消'
            }
        });
        if (result.length > 0) {
            res.send({
                status: 200,
                result: result
            })
        } else {
            res.send({
                status: 500,
                result: result,
                msg: "暂无数据"
            })
        }
    })
})


router.get("/returned_search", (req, res) => {
    console.log('query:', req.query)
    console.log('传过来的参数:', req.query.search_word)
    var search = req.query.search_word;
    const sql = "select * from orders where concat(`order_number`,`user_name`,`goods_name`) like '%" + search + "%' and order_status = 5";
    sqlFn(sql, null, (result) => {
        console.log('result:', result)
        console.log('result长度:', result.length)
        result.forEach(el => {
            el.order_status='退货'
        });
        if (result.length > 0) {
            res.send({
                status: 200,
                result: result
            })
        } else {
            res.send({
                status: 500,
                result: result,
                msg: "暂无数据"
            })
        }
    })
})

router.post('/order_updateProgress', (req, res) => {
    console.log('req:', req.query);
    const sql = "update orders set order_message=? where id=?";
    let arr = [req.query.detail, req.query.id]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '修改成功',
        });
    })
});


router.post('/agreeReturn', (req, res) => {
    console.log('req:', req.query);
    const sql = "update orders set handle_time=?,returned_status=? where id=?";
    let arr = [req.query.handleTime,2,req.query.id]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '修改成功',
        });
    })
});


router.post('/OrderStatus_submit', (req, res) => {
    console.log('req:', req.query);
    const sql = "update orders set order_status=?,reason=?,apply_time=?,returned_status=? where id=?";
    let arr = [req.query.status,req.query.reason,req.query.applyTime,1,Number(req.query.id)]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '修改订单状态成功',
        });
    })
});
router.post('/users_deleteSelect', (req, res) => {
    console.log(req.query.data)
    let data = JSON.parse(req.query.data);
    const sql = "delete from users where id=?";

    data.forEach(el => {
        let users_id = [Number(el.id)]
        sqlFn(sql, users_id, (result) => {
            console.log('result:', result)
        })
    });
    res.send({
        res_code: '删除成功',
    });

});

module.exports = router